package org.chen.bossTimer.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.chen.bossTimer.bean.Boss;
import org.chen.bossTimer.mapper.BossMapper;
import org.chen.bossTimer.response.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/api/boss")
public class BossControl {
	@Autowired
	private BossMapper bossMapper;

	private final static Logger logger = LoggerFactory.getLogger(BossControl.class);

	@GetMapping("/list")
	public Object getBosslist(HttpServletRequest req) {
		logger.info("获取boss列表");
		List<Boss> Bosslist = bossMapper.getBossList();
		return Bosslist;
	}

	@PostMapping(value = "/boss")
	@ResponseBody
	public Object addBoss(HttpServletRequest req, @RequestBody Boss boss) throws Exception {
		logger.info("添加一个Boss");
		logger.debug(boss.toString());
		int res = bossMapper.addBoss(boss);
		if (res == 1) {
			return ResultData.success("添加成功");
		} else {
			return ResultData.fail(-1, "添加失败");
		}
	}

	@DeleteMapping("/boss")
	@ResponseBody
	public Object delBoss(HttpServletRequest req, @RequestBody Map<String, int[]> params) {
		logger.info("删除Boss");
		logger.debug(params.toString());
		try {
			int[] idList = params.get("id");
			for (int i : idList) {
				bossMapper.deleteBoss(i);
			}
		} catch (Exception e) {
			return ResultData.fail(400, "删除失败");
		}
		return ResultData.success("删除成功");
	}

	@PutMapping("/boss")
	@ResponseBody
	public Object updateTask(HttpServletRequest req, @RequestBody Boss boss) {
		logger.info("更新boss");
		logger.debug(boss.toString());
		int code = bossMapper.updateBoss(boss);
		if (code == 1) {
			return ResultData.success("修改成功");
		} else {
			return ResultData.fail(400, "修改失败");
		}
	}
}
